import settingData from './data'

export default ({
    namespaced:true,
    state: {
         //设置状态
         SettingState:false,

         SettingIndex:1,

         //大小 0:默认 1:全屏
         SettingBoxSize:-1,

         // 背景图片 默认白天模式
         BackImage:JSON.parse(localStorage.getItem('BackImage'))|| {label:'Day.jpg',src:'Day.jpg',type:1},
         

         //系统自带图片
         BackImages:settingData.BackImages,
          
         //主题
         SubjectData:settingData.SubjectData,

         //当前主题
         Subject:settingData.Subject,

         BackColor:[
           JSON.parse(localStorage.getItem('Navigation'))||{BackColor:null,Opacity:100,name:'Navigation',label:'1.导航条背景色'},
           JSON.parse(localStorage.getItem('Article'))||{BackColor:null,Opacity:100,name:'Article',label:'2.文章背景色'},
         ],

          
        
         //导航条
         Navigation:JSON.parse(localStorage.getItem('Navigation'))||{BackColor:null,Opacity:100,name:'Navigation'},
                   
          //文章
         Article:JSON.parse(localStorage.getItem('Article')) ||{BackColor:null,Opacity:100,name:'Article'},

    
         //可选择的颜色
         Colors:JSON.parse(localStorage.getItem('Colors')) || ['#ffffff','#808080','#3CC5BC','#5C5893'],
         
         //修改颜色界面的状态
         ColorState:false,

         //雪花特效 0|1
         SnowState:localStorage.getItem('SnowState') || 1,

          
    },
    getters: {},
    mutations: {
       //开启设置
       SETTINGON:(state)=> state.SettingState=true,
       //关闭设置
       SETTINGDOWN:(state)=>{ state.SettingBoxSize=0 ,state.SettingState=false},

        //开启增添颜色
        COLORON:(state)=> state.ColorState=true,
        //关闭增添颜色
        COLORDOWN:(state)=>state.ColorState=false,

        //开启雪花特效
       SNOWON:(state)=> {
         state.SnowState=1
         localStorage.setItem('SnowState',1)
       },
        //关闭雪花特效
        SNOWDOWN:(state)=>{
         state.SnowState=0
         localStorage.setItem('SnowState',0)
        },
      
      //增加颜色
      ADDCOlOR:(state,color) =>{
         state.Colors.push(color)
         localStorage.setItem('Colors',JSON.stringify(state.Colors))
      },
      //删除颜色
      DELETECOlOR:(state,color) =>{
         if(color){
            let index=state.Colors.findIndex(item => item==color)
            state.Colors.splice(index,1)
            localStorage.setItem('Colors',JSON.stringify(state.Colors))
         }    
      },
      //恢复默认
      REFRESHCOLOR(state){
         localStorage.removeItem('Colors')
         state.Colors= ['#ffffff','#808080','#3CC5BC','#5C5893']
      },


      //设置背景图片
      SETBACKIMAGE:(state,data)=> {
         state.BackImage.type=data.type
         state.BackImage.src=data.src
         state.BackImage.label=data.label
         localStorage.setItem('BackImage',JSON.stringify(state.BackImage))
      },
      //设置的导航
      SETSETTINGINDEX:(state,index)=>state.SettingIndex=index,
       
      //设置盒子大小 0:默认 1:全屏
      SETSETTINGBOXSIZE:(state,index) => state.SettingBoxSize=index,

      //
      SETBACKCOLOR:(state,data)=>{
           state.BackColor.forEach((item)=>{
                if(item.name==data.name){   
                   item=Object.assign(item,data)
                   localStorage.setItem(item.name,JSON.stringify(item))
                   state[item.name]=Object.assign(state[item.name],data)
                   return
                }
            })      
      },
      //刷新
      REFRESHBACKCOLOR:(state,name)=>{
         localStorage.removeItem(name)
         state.BackColor=[
            JSON.parse(localStorage.getItem('Navigation'))||{BackColor:null,Opacity:100,name:'Navigation',label:'1.导航条背景色'},
            JSON.parse(localStorage.getItem('Article'))||{BackColor:null,Opacity:100,name:'Article',label:'2.文章背景色'},
          ]
          let newData=state.BackColor.find( (item) => item.name == name)
          state[name]=Object.assign(state[name],newData)
      },
      //设置主题
      SETSUBJECT:(state,data)=>{
      
         state.Subject ={...state.Subject,...data}
         localStorage.setItem('Subject',JSON.stringify(state.Subject))

         //设置背景
         state.BackImage.type=1
         state.BackImage.src=data.src
         state.BackImage.label=data.src
         localStorage.setItem('BackImage',JSON.stringify(state.BackImage))
       
         //设置个性组件
         state.BackColor.forEach(item =>{
            item.BackColor=data.GeneralBackColor
            item.Opacity=data.Opacity || 100
            state[item.name]=Object.assign(state[item.name],item)
            localStorage.setItem(item.name,JSON.stringify(item))
         })
      },

      //设置字体
      SETFAMILY:(state,family) => {
         state.Subject.fontFamily=family
         localStorage.setItem('Subject',JSON.stringify(state.Subject))
      }
    },
})

